package com.yeyks.controller.room;

import com.alibaba.fastjson.JSON;
import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.account.utils.AccountUtil;
import com.yeyks.annotation.Login;
import com.yeyks.annotation.LoginUser;
import com.yeyks.annotation.NeedToken;
import com.yeyks.annotation.SystemLog;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.base.Arguments;
import com.yeyks.common.constants.AccountType;
import com.yeyks.common.constants.SysLogMsg;
import com.yeyks.common.dto.mq.EsUpdateMessage;
import com.yeyks.common.em.ResponseStatus;
import com.yeyks.common.em.RoomBookingRules;
import com.yeyks.common.exception.PermissionInvalidException;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.exception.TokenInvalidException;
import com.yeyks.common.utils.webutil.Utility;
import com.yeyks.commonReference.append.rocketmq.service.producer.MQProducerService;
import com.yeyks.commonReference.service.account.AccountInfoService;
import com.yeyks.commonReference.service.hotel.HotelInfoService;
import com.yeyks.commonReference.service.hotel.RoomInfoService;
import com.yeyks.hotel.dal.dao.RoomInfoMapper;
import com.yeyks.hotel.dal.domain.HotelInfo;
import com.yeyks.hotel.dal.domain.RoomCalendarInfo;
import com.yeyks.hotel.dal.domain.RoomInfo;
import com.yeyks.hotel.param.UpdateRoomCalendarBatchParam;
import com.yeyks.hotel.service.RoomCalendarInfoService;
import com.yeyks.web.AccountUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "酒店房间模块")
@RestController
@Slf4j
@RequestMapping("/ajax/room")
public class RoomController {

    @Autowired
    private AccountInfoService accountService;
    @Autowired
    private HotelInfoService hotelService;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private RoomInfoService roomInfoService;
    @Autowired
    private RoomCalendarInfoService roomCalendarInfoService;
    @Autowired
    private MQProducerService mQProducerService;

    /**
     * 查询酒店房间信息
     *
     * @return
     */
    @ApiOperation(value = "酒店客房列表", notes = "酒店客房列表")
    @RequestMapping(value = "/searchRoom", method = RequestMethod.POST)
    @Login
    public ResponseEntity<?> searchRoom(@ApiIgnore @LoginUser AccountInfo loginUser) {
        AjaxResponseBody responseBody = new AjaxResponseBody();
        if (loginUser.getType() != AccountType.HotelManager) {
            throw new ServiceException("无权限");
        }
        Integer loginUserId = loginUser.getId();
        if (null != loginUser.getManagerId()) {
            loginUserId = loginUser.getManagerId();
        }
        HotelInfo hotelInfo = hotelService.searchHotelByOperator(loginUserId);
        if (hotelInfo == null) {
            responseBody.setCode(ResponseStatus.RS_ROOM_NO_HOTEL_INFO.getCode());
            responseBody.setMsg(ResponseStatus.RS_ROOM_NO_HOTEL_INFO.getMsg());
        } else {
            List<RoomInfo> roomInfoList = roomInfoService.searchRoomByHotelId(hotelInfo.getId());
            responseBody.setBody(roomInfoList);
            responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
            responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
        }
        return ResponseEntity.ok(responseBody);
    }

    /**
     * 增加房型
     *
     * @param token
     * @param roomInfo
     * @return
     */
    @Login
    @SystemLog(value = SysLogMsg.ROOM_ADD)
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResponseEntity<?> add(@RequestParam(name = "token", defaultValue = "") String token, RoomInfo roomInfo) {
        AjaxResponseBody responseBody = new AjaxResponseBody();
        checkout(roomInfo);
        try {
            AccountInfo loginUser = accountService.token2AccountInfo(token);

            // 查询酒店管理员所对应管理的酒店信息
            HotelInfo hotelInfo = hotelService.searchHotelByOperator(AccountUtil.getOperationUserId(loginUser));
            if (hotelInfo != null) {
                // 查询房间是否已存在
                List<RoomInfo> roomInfoList = roomInfoMapper.selectByHotelIdAndTitle(hotelInfo.getId(), roomInfo.getTitle());
                if (roomInfoList == null || roomInfoList.size() == 0) {
                    roomInfo.setHotelId(hotelInfo.getId());
                    RoomInfo newRoomInfo = roomInfoService.addRoomInfo(roomInfo);
                    responseBody.setBody(newRoomInfo.getId());
                    responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
                    responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
                } else {
                    responseBody.setBody(roomInfoList);
                    responseBody.setCode(ResponseStatus.RS_FAIL_DUPLICATE.getCode());
                    responseBody.setMsg(ResponseStatus.RS_FAIL_DUPLICATE.getMsg());
                }
            } else {
                responseBody.setCode(ResponseStatus.RS_ROOM_NO_HOTEL_INFO.getCode());
                responseBody.setMsg(ResponseStatus.RS_ROOM_NO_HOTEL_INFO.getMsg());
            }
        } catch (DuplicateKeyException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_DUPLICATE.getCode());
            responseBody.setMsg(e.getMessage());
        } catch (Throwable e) {
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(e.getMessage());
        }
        //发送mq信息
        sendRoomMqMessage(roomInfo.getId());
        return ResponseEntity.ok(responseBody);
    }

    private void checkout(RoomInfo roomInfo) {
        if (Arguments.strNotNull(roomInfo.getTitle()) && roomInfo.getTitle().length() >= 30) {
            throw new ServiceException("标题太长，请控制在30字内");
        }
        if (Arguments.isNullOrZero(roomInfo.getCurPrice())
                || roomInfo.getCurPrice() >= 1000000) {
            throw new ServiceException("售价输入非法");
        }
        if (Arguments.isNullOrZero(roomInfo.getMaxPeople())
                || roomInfo.getMaxPeople() > 99) {
            throw new ServiceException("入住人数输入非法");
        }
        if (roomInfo.getFloor() != null && roomInfo.getFloor() > 1000) {
            throw new ServiceException("楼层数不能大于1000");
        }
        if (roomInfo.getCount() != null && roomInfo.getCount() > 1000) {
            throw new ServiceException("请输入合适的房间数");
        }
    }

    /**
     * 修改房间的上架状态
     *
     * @param roomIds
     * @param valid
     * @return
     */
    @NeedToken
    @SystemLog(value = SysLogMsg.ROOM_UPDATE_VALID)
    @ApiOperation(value = "酒店房间上下架")
    @RequestMapping(value = "/updateValid", method = RequestMethod.POST)
    public ResponseEntity<?> updateValid(@RequestParam(name = "roomIds", defaultValue = "", required = true) String roomIds,
                                         @RequestParam(name = "valid", defaultValue = "true", required = true) Boolean valid) {
        AjaxResponseBody responseBody = new AjaxResponseBody();
        AccountInfo operatorInfo = AccountUtils.getLoginUser();
        List<Integer> ids = Utility.str2intList(roomIds);
        int status = valid ? 1 : 0;
        try {
            if (!accountService.roomHavePermission(AccountUtil.getOperationUserId(operatorInfo), ids)) {
                //如果是总公司
                if (operatorInfo.getType().equals(AccountType.Administrator)) {
                    roomInfoMapper.updateRoomValid(ids, valid);
                    roomCalendarInfoService.updateRoomCalendarStatusList(ids, status);
                    responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
                    responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
                } else {
                    responseBody.setCode(ResponseStatus.RS_ROOM_NOT_MATCH_HOTEL.getCode());
                    responseBody.setMsg(ResponseStatus.RS_ROOM_NOT_MATCH_HOTEL.getMsg());
                }
            } else {
                roomInfoMapper.updateRoomValid(ids, valid);
                roomCalendarInfoService.updateRoomCalendarStatusList(ids, status);
                responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
                responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
            }
        } catch (DuplicateKeyException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_DUPLICATE.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_DUPLICATE.getMsg());
        } catch (Throwable e) {
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL.getMsg());
        }
        //发送mq信息
        List<String> sourceIds = new ArrayList<>();
        for(Integer id:ids) {
            sourceIds.add(String.valueOf(id));
            sendRoomMqMessage(id);
        }
        mQProducerService.sendEsUpdate(new EsUpdateMessage("room","yyks",sourceIds), 0);
        return ResponseEntity.ok(responseBody);
    }

    /**
     * 获取酒店的退款配置信息
     *
     * @param roomInfo
     * @return
     */
    @RequestMapping(value = "/getRefundCfg", method = RequestMethod.POST)
    public ResponseEntity<?> getRefundCfg(RoomInfo roomInfo) {
        AjaxResponseBody responseBody = new AjaxResponseBody();
        try {
            if (roomInfo.getHotelId() == null || roomInfo.getHotelId() == 0) {
                if (roomInfo.getId() == null || roomInfo.getId() == 0) {
                    throw new InvalidParameterException("invalid parameters id == 0");
                }
                roomInfo = roomInfoMapper.selectByPrimaryKey(roomInfo.getId());
                if (roomInfo == null) {
                    throw new InvalidParameterException("cannot find room information");
                }
            }
            if (roomInfo.getHotelId() == null || roomInfo.getHotelId() == 0) {
                throw new InvalidParameterException("invalid parameters hotelId == 0");
            }
            HotelInfo hotelInfo = hotelService.selectHotelInfoById(roomInfo.getHotelId());
            if (hotelInfo == null) {
                throw new InvalidParameterException("cannot find hotel information");
            }
            responseBody.setBody(hotelInfo.getRefundCfg());
            responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
            responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
        } catch (InvalidParameterException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_PARAMETERS.getCode());
            responseBody.setMsg(e.getMessage());
        } catch (Throwable e) {
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL.getMsg());
        }
        return ResponseEntity.ok(responseBody);
    }

    /**
     * 修改房间信息
     *
     * @param token
     * @param roomInfo
     * @return
     */
    @SystemLog(value = SysLogMsg.ROOM_UPDATE_MSG)
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    public ResponseEntity<?> modify(@RequestParam(name = "token", defaultValue = "") String token,
                                    RoomInfo roomInfo) {
        AjaxResponseBody responseBody = new AjaxResponseBody();
        AccountInfo accountInfo = accountService.token2AccountInfo(token);
        try {
            if (accountInfo.getType() == AccountType.User) {
                throw new PermissionInvalidException();
            } else if (accountInfo.getType() == AccountType.HotelManager) {
                RoomInfo origRoomInfo = roomInfoMapper.selectByPrimaryKey(roomInfo.getId());
                HotelInfo hotelInfo = hotelService.searchHotelByOperator(AccountUtil.getOperationUserId(accountInfo), origRoomInfo.getHotelId());

                if (hotelInfo == null) {
                    responseBody.setCode(ResponseStatus.RS_ROOM_HOTEL_ID_NE.getCode());
                    responseBody.setMsg(ResponseStatus.RS_ROOM_HOTEL_ID_NE.getMsg());
                } else {
                    if (origRoomInfo.getTodayTime() != null) {
                        roomInfo.setTodayTime(origRoomInfo.getTodayTime());
                    }
                    roomInfoMapper.updateByPrimaryKey(roomInfo);
                    responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
                    responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
                }
            } else {
                RoomInfo origRoomInfo = roomInfoMapper.selectByPrimaryKey(roomInfo.getId());
                roomInfo.setHotelId(origRoomInfo.getHotelId());
                if (origRoomInfo.getTodayTime() != null) {
                    roomInfo.setTodayTime(origRoomInfo.getTodayTime());
                }
                roomInfoMapper.updateByPrimaryKey(roomInfo);
                responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
                responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
            }
        } catch (DuplicateKeyException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_DUPLICATE.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_DUPLICATE.getMsg());
        } catch (Throwable e) {
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL.getMsg());
        }
        sendRoomMqMessage(roomInfo.getId());
        return ResponseEntity.ok(responseBody);
    }

    private void putExtraInfo(Integer roomId, String key, String value) throws Exception {
        RoomInfo origRoomInfo = roomInfoMapper.selectByPrimaryKey(roomId);
        if (origRoomInfo == null) {
            throw new InvalidParameterException();
        }
        String extraInfo = Utility.setExtraInfo(origRoomInfo.getExtraInfo(), key, value);
        int rec = roomInfoMapper.updateExtraInfo(roomId, extraInfo);
        if (rec == 0) {
            throw new Exception("Updated 0 rows");
        }
    }

    private String getExtraInfo(Integer roomId, String key) {
        RoomInfo origRoomInfo = roomInfoMapper.selectByPrimaryKey(roomId);
        if (origRoomInfo == null) {
            throw new InvalidParameterException();
        }
        String rule = Utility.getExtraInfo(origRoomInfo.getExtraInfo(), key);
        return Arguments.strNotNull(rule) ? "{}" : rule;
    }

    /**
     * 设置房间的开放时间规则
     *
     * @param token
     * @param roomId
     * @param rule
     * @param available
     * @return
     */
    @SystemLog(value = SysLogMsg.ROOM_UPDATE_RULE)
    @RequestMapping(value = "/setBookingRule", method = RequestMethod.POST)
    public ResponseEntity<?> setBookingRule(@RequestParam(name = "token", defaultValue = "") String token,
                                            @RequestParam(name = "roomId", defaultValue = "") Integer roomId,
                                            @RequestParam(name = "rule", defaultValue = "") String rule,
                                            @RequestParam(name = "available", defaultValue = "") Integer available) {
        AjaxResponseBody responseBody = new AjaxResponseBody();
        if (!Arguments.strNotNull(rule)) {
            throw new ServiceException("房间开放规则不能为空");
        }
        try {
            AccountInfo accountInfo = accountService.token2AccountInfo(token);
            if (accountInfo.getType() != AccountType.HotelManager) {
                throw new PermissionInvalidException();
            } else {
                RoomBookingRules bookingRules = JSON.parseObject(rule, RoomBookingRules.class);
                if (bookingRules == null) {
                    throw new InvalidParameterException("booking rule error");
                }
                RoomInfo origRoomInfo = roomInfoMapper.selectByPrimaryKey(roomId);
                if (origRoomInfo == null) {
                    throw new InvalidParameterException("cannot find room information.");
                }
                String extraInfo = Utility.setExtraInfo(origRoomInfo.getExtraInfo(), "booking_rule", rule);
                RoomInfo roomInfo = new RoomInfo();
                roomInfo.setId(roomId);
                roomInfo.setExtraInfo(extraInfo);
                roomInfo.setAvailable(available);
                int rec = roomInfoMapper.updateIfNotNullByPrimaryKey(roomInfo);

                if (rec == 0) {
                    throw new Exception("Updated 0 rows");
                }
                responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
                responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
            }
        } catch (TokenInvalidException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_TOKEN.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_INVALID_TOKEN.getMsg());
        } catch (InvalidParameterException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_PARAMETERS.getCode());
            responseBody.setMsg(e.getMessage());
        } catch (DuplicateKeyException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_DUPLICATE.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_DUPLICATE.getMsg());
        } catch (Throwable e) {
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(e.getMessage());
        }
        sendRoomMqMessage(roomId);
        return ResponseEntity.ok(responseBody);
    }

    /**
     * 获取房间的开放时间规则
     *
     * @param token
     * @param roomId
     * @return
     */
    @RequestMapping(value = "/getBookingRule", method = RequestMethod.POST)
    public ResponseEntity<?> getBookingRule(@RequestParam(name = "token", defaultValue = "") String token,
                                            @RequestParam(name = "roomId", defaultValue = "") Integer roomId) {
        AjaxResponseBody responseBody = new AjaxResponseBody();
        try {
            AccountInfo accountInfo = accountService.token2AccountInfo(token);
            if (accountInfo.getType() != AccountType.HotelManager) {
                throw new PermissionInvalidException();
            } else {
                responseBody.setBody(getExtraInfo(roomId, "booking_rule"));
                responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
                responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
            }
        } catch (DuplicateKeyException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_DUPLICATE.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_DUPLICATE.getMsg());
        } catch (Throwable e) {
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(e.getMessage());
        }
        return ResponseEntity.ok(responseBody);
    }

    /**
     * 增加房间的扩展信息
     *
     * @param token
     * @param roomId
     * @param key
     * @param value
     * @return
     */
    @RequestMapping(value = "/putExtraInfo", method = RequestMethod.POST)
    public ResponseEntity<?> putExtraInfo(
            @RequestParam(name = "token", defaultValue = "") String token,
            @RequestParam(name = "roomId", defaultValue = "") Integer roomId,
            @RequestParam(name = "key", defaultValue = "") String key,
            @RequestParam(name = "value", defaultValue = "") String value) {
        AjaxResponseBody responseBody = new AjaxResponseBody();
        try {
            AccountInfo accountInfo = accountService.token2AccountInfo(token);
            if (accountInfo.getType() != AccountType.HotelManager) {
                throw new PermissionInvalidException();
            } else {
                putExtraInfo(roomId, key, value);
                responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
                responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
            }
        } catch (TokenInvalidException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_TOKEN.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_INVALID_TOKEN.getMsg());
        } catch (DuplicateKeyException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_DUPLICATE.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_DUPLICATE.getMsg());
        } catch (Throwable e) {
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(e.getMessage());
        }
        sendRoomMqMessage(roomId);
        return ResponseEntity.ok(responseBody);
    }

    /**
     * 获取房间的扩展信息
     *
     * @param token
     * @param roomId
     * @param key
     * @return
     */
    @RequestMapping(value = "/getExtraInfo", method = RequestMethod.POST)
    public ResponseEntity<?> getExtraInfo(
            @RequestParam(name = "token", defaultValue = "") String token,
            @RequestParam(name = "roomId", defaultValue = "") Integer roomId,
            @RequestParam(name = "key", defaultValue = "") String key) {
        AjaxResponseBody responseBody = new AjaxResponseBody();
        try {
            AccountInfo accountInfo = accountService.token2AccountInfo(token);
            if (accountInfo.getType() != AccountType.HotelManager) {
                throw new PermissionInvalidException();
            } else {
                responseBody.setBody(getExtraInfo(roomId, key));
                responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
                responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
            }
        } catch (DuplicateKeyException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_DUPLICATE.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_DUPLICATE.getMsg());
        } catch (Throwable e) {
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(e.getMessage());
        }
        return ResponseEntity.ok(responseBody);
    }

    /**
     * 删除房间信息
     *
     * @param token
     * @param roomInfo
     * @return
     */
    @SystemLog(value = SysLogMsg.ROOM_DEL)
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ResponseEntity<?> del(@RequestParam(name = "token", defaultValue = "") String token, RoomInfo roomInfo) {
        AjaxResponseBody responseBody = new AjaxResponseBody();
        try {
            AccountInfo accountInfo = accountService.token2AccountInfo(token);
            if (accountInfo.getType() == AccountType.User) {
                throw new PermissionInvalidException();
            } else if (accountInfo.getType() == AccountType.HotelManager) {
                RoomInfo origRoomInfo = roomInfoMapper.selectByPrimaryKey(roomInfo.getId());
                HotelInfo hotelInfo = hotelService.searchHotelByOperator(accountInfo.getId(), origRoomInfo.getHotelId());
                if (hotelInfo == null) {
                    responseBody.setCode(ResponseStatus.RS_ROOM_HOTEL_ID_NE.getCode());
                    responseBody.setMsg(ResponseStatus.RS_ROOM_HOTEL_ID_NE.getMsg());
                } else {
                    roomInfoService.deleteById(roomInfo.getId(), 0);
                    //修改房态的状态为0
                    responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
                    responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
                }
            } else {
                roomInfoService.deleteById(roomInfo.getId(), 0);
                //修改房态的状态为0
                responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
                responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
            }
        } catch (DuplicateKeyException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_DUPLICATE.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_DUPLICATE.getMsg());
        } catch (Throwable e) {
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL.getMsg());
        }
        sendRoomMqMessage(roomInfo.getId());
        return ResponseEntity.ok(responseBody);
    }

    @ApiOperation(value = "查询房态信息", notes = "查询房态信息")
    @RequestMapping(value = "/selectRoomCalendar", method = RequestMethod.POST)
    public AjaxResponseBody selectRoomCalendar(@RequestParam(name = "roomId", defaultValue = "") Integer roomId) {

        List<RoomCalendarInfo> list = roomCalendarInfoService.selectRoomCalendar(roomId);

        return AjaxResponseBody.success(list);
    }

    @ApiOperation(value = "初始化房态信息", notes = "初始化房态信息")
    @RequestMapping(value = "/initRoomCalendar", method = RequestMethod.POST)
    public AjaxResponseBody initRoomCalendar(@RequestParam(name = "roomId", defaultValue = "") Integer roomId,
                                             @RequestParam(name = "days", defaultValue = "30") Integer days) {
        RoomInfo roomInfo = roomInfoService.selectByPrimaryKey(roomId);
        if (null != roomInfo) {
            roomCalendarInfoService.initRoomCalendar(roomInfo);
            return AjaxResponseBody.success(roomInfo);
        }
        sendRoomMqMessage(roomInfo.getId());
        return AjaxResponseBody.failed("失败");
    }

    @SystemLog(value = SysLogMsg.ROOM_UPDATE_CONFIG)
    @ApiOperation(value = "修改房间配置信息", notes = "修改房间配置信息")
    @RequestMapping(value = "/updateRoomConfig", method = RequestMethod.POST)
    @Login
    public AjaxResponseBody updateRoomConfig(
            @RequestParam(name = "roomId", defaultValue = "") Integer roomId,
            @RequestParam(name = "salePrice", defaultValue = "") Integer salePrice,
            @RequestParam(name = "bottomPrice", defaultValue = "") Integer bottomPrice,
            @RequestParam(name = "count", defaultValue = "") Integer count) {
        RoomInfo roomInfo = roomInfoService.getById(roomId);
        if (null == roomInfo) {
            return AjaxResponseBody.failed("对象不存在");
        }
        roomInfo.setCurPrice(salePrice);
        roomInfo.setBottomPrice(bottomPrice);
        roomInfo.setCount(count);
        boolean result = roomInfoService.updateById(roomInfo);
        sendRoomMqMessage(roomInfo.getId());
        return AjaxResponseBody.success(result);
    }

    @ApiOperation(value = "修改单日房态信息", notes = "修改单日房态信息")
    @RequestMapping(value = "/updateRoomCalendarSingle", method = RequestMethod.POST)
    @Login
    public AjaxResponseBody updateRoomCalendarSingle(
            @RequestParam(name = "id", defaultValue = "") Integer id,
            @RequestParam(name = "salePrice", defaultValue = "") Integer salePrice,
            @RequestParam(name = "bottomPrice", defaultValue = "") Integer bottomPrice,
            @RequestParam(name = "count", defaultValue = "") Integer count,
            @RequestParam(name = "status", defaultValue = "") Integer status) {
        RoomCalendarInfo roomCalendarInfo = roomCalendarInfoService.getById(id);
        if (null == roomCalendarInfo) {
            return AjaxResponseBody.failed("对象不存在");
        }
        roomCalendarInfo.setSalePrice(salePrice);
        roomCalendarInfo.setBottomPrice(bottomPrice);
        roomCalendarInfo.setCount(count);
        roomCalendarInfo.setStatus(status);

        AjaxResponseBody res = roomCalendarInfoService.updateRoomCalendar(roomCalendarInfo);
        //发送mq
        sendCalendarMqMessage(id);
        return res;
    }

    @ApiOperation(value = "批量更新房态信息", notes = "批量更新房态信息")
    @RequestMapping(value = "/updateRoomCalendarBatch", method = RequestMethod.POST)
    @Login
    public AjaxResponseBody updateRoomCalendarBatch(UpdateRoomCalendarBatchParam updateRoomCalendarBatchParam, @ApiIgnore @LoginUser AccountInfo accountInfo) {
        if (AccountType.HotelManager != accountInfo.getType() && AccountType.Administrator != accountInfo.getType()) {
            return AjaxResponseBody.failed("账号无操作权限");
        }
        updateRoomCalendarBatchParam.setHotelId(updateRoomCalendarBatchParam.getHotelId());
        AjaxResponseBody res = roomCalendarInfoService.updateRoomCalendarBatch(updateRoomCalendarBatchParam);
        //发送mq信息
        sendRoomMqMessage(updateRoomCalendarBatchParam.getRoomId());
        return res ;
    }


    /**
     * 发送酒店修改信息到mq
     * @param roomId
     */
    private void sendRoomMqMessage(Integer roomId)
    {
        List<String> sourceIds = new ArrayList<>();
        sourceIds.add(String.valueOf(roomId));
        mQProducerService.sendEsUpdate(new EsUpdateMessage("room","yyks",sourceIds), 0);
    }

    /**
     * 发送房态id修改
     * @param calendarId
     */
    private void sendCalendarMqMessage(Integer calendarId)
    {
        List<String> sourceIds = new ArrayList<>();
        sourceIds.add(String.valueOf(calendarId));
        mQProducerService.sendEsUpdate(new EsUpdateMessage("calendar","yyks",sourceIds), 0);
    }
}
